package j.framework.qmd.supported;

import j.framework.core.LoginUser;
import j.framework.qmd.core.QueryDataHandler;
import j.framework.qmd.core.QueryFieldMetadata;
import j.framework.qmd.core.QueryMetadata;
import j.framework.qmd.core.QueryResult;
import j.util.DES;
import j.util.StringUtils;
import j.util.jdbc.QueryJdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * User: jason
 * Date: 2014/11/21 16:50
 */
public class FieldFooterQueryHandler implements QueryDataHandler {
    @Override
    public Object getColumnValue(QueryFieldMetadata fieldMetadata, Object value, Map<String, Object> rowData) {
        return value;
    }

    @Override
    public String buildWhereClause(LoginUser user, Map<String, String> queryParams, List<Object> args) {
        if(StringUtils.isNullOrEmpty(queryParams.get("fieldId"))){
            return null;
        }
        String where = "ff.query_field_id=?";
        args.add(queryParams.get("fieldId"));
        return where;
    }

    @Override
    public void prepareCall(LoginUser user, Map<String, String> queryParams, Map<String, Object> callParams) {

    }

    /**
     * 获取操作
     *
     * @param user
     * @param rowData
     * @return
     */
    @Override
    public String getOperatorHtml(LoginUser user, Map<String, Object> rowData) {
        StringBuilder s = new StringBuilder();
        s.append(String.format("<a href=\"javascript:j.open('/user/req?action=edit&rid=%s&footerId=%s&key=%s','editWin',null,600,500);\">修改</a>",
                DES.encrypt("REQ00000000000000011"),getDesId(rowData),"query_field_footer"));

        s.append(String.format(" <a href=\"javascript:delFooter('%s')\">删除</a>",
                getDesId(rowData)));

        return s.toString();
    }

    /**
     * 执行查询前将会被调用，若返回true，仍执行框架查询
     *
     * @param qjt
     * @param qmd
     * @param queryParams
     * @param queryResult 执行的结果
     * @return
     */
    @Override
    public boolean beforeQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {
        return true;
    }

    /**
     * 事后查询
     *
     * @param qjt
     * @param qmd
     * @param queryParams
     * @param queryResult
     */
    @Override
    public void doAfterQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {

    }

    private String getDesId(Map<String,Object> rowData){
        return (String)rowData.get("desId");
    }
}
